package com.ruoyi.controller.team;

import com.ruoyi.center.service.INoticeCenterService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.service.IMgOrderService;
import com.ruoyi.pay.manager.service.IPayManagerService;
import com.ruoyi.team.domain.vo.MgTeam;
import com.ruoyi.team.domain.vo.TeamSignExport;
import com.ruoyi.team.service.ITeamSignService;
import com.ruoyi.user.domain.MgUser;
import com.ruoyi.user.service.IMgUserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 团队Controller
 *
 * @author ruoyi
 * @date 2024-01-08
 */
@RestController
@RequestMapping("/team/sign")
public class TeamSignController extends BaseController {
    @Autowired
    private ITeamSignService teamSignService;

    private IMgUserService mgUserService;
    
    INoticeCenterService noticeCenterService;
    
    IPayManagerService payManagerService;
    
    IMgOrderService orderService;
    
    
    @Resource
	public void setNoticeCenterService(INoticeCenterService noticeCenterService) {
		this.noticeCenterService = noticeCenterService;
	}
    
	@Resource
    public void setPayManagerService(IPayManagerService payManagerService) {
		this.payManagerService = payManagerService;
	}
    @Resource
	public void setOrderService(IMgOrderService orderService) {
		this.orderService = orderService;
	}

	/**
     * 查询团队列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MgTeam mgTeam) {
        startPage();
        List<MgTeam> list = teamSignService.selectTeamList(mgTeam);
        return getDataTable(list);
    }

    /**
     * 导出团队列表
     */
    @Log(title = "团队", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MgTeam mgTeam)
    {
        List<MgTeam> list = teamSignService.selectTeamList(mgTeam);
        List<TeamSignExport> teamSignExports = new ArrayList<>();
        list.forEach(x -> {
            TeamSignExport tar = new TeamSignExport();
            BeanUtils.copyProperties(x, tar);
            
            teamSignExports.add(tar);
        });
        ExcelUtil<TeamSignExport> util = new ExcelUtil<TeamSignExport>(TeamSignExport.class);
        util.exportExcel(response, teamSignExports, "团队签约");
    }
//
    /**
     * 获取团队详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(teamSignService.selectTeamById(id));
    }

    /**
     * 修改团队
     */
    @Log(title = "团队", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody MgTeam mgTeam)
    {
        Integer id = mgTeam.getId();
		MgTeam selectTeamById = this.teamSignService.selectTeamById(id);

        if (null == selectTeamById) {
        	return AjaxResult.error("团队不存在");
        }
        MgTeam data =new MgTeam();
        data.setId(id);
        data.setVerify(mgTeam.getVerify());
        String status = mgTeam.getStatus();
		data.setStatus(status);

        teamSignService.updateMgTeam(data);

        noticeCenterService.teamNoticed(Integer.valueOf(status), id, selectTeamById.getUserId().intValue(), mgTeam.getVerify());
        
        if ("2".equals(status)) {
        	// 驳回
        	return success();
        }
        
        MgUser mgUser = new MgUser();

        mgUser.setId(Integer.valueOf(String.valueOf(selectTeamById.getUserId())));

        mgUser.setTeamId(Long.valueOf(selectTeamById.getId()));

        mgUser.setStatus("1");
        
        mgUser.setIsRegimental((byte) 1);
        mgUser.setJoinTime(Long.valueOf(System.currentTimeMillis() / 1000));
        
        // 更新团长订单teamId
//        int teamId = mgUser.getTeamId().intValue();
//		int userId = mgUser.getId().intValue();
//		orderService.updateTeamId(teamId, userId);

        // 更新支付管理
//        payManagerService.updateTeamId(teamId, userId);
        
        return toAjax(mgUserService.updateMgUser(mgUser));
    }

    @Resource
	public void setMgUserService(IMgUserService mgUserService) {
		this.mgUserService = mgUserService;
	}

}
